package com.itheima.health.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * @author ：seanyang
 * @date ：Created in 2019/6/12
 * @description ：套餐Controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/mobile/setmeal")
@Slf4j
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    /**
     * @description：引入Redis
     * @author:jiangdp
     * @Date:Created in 2019/7/29
     */
    @Autowired
    private JedisPool jedisPool;

    /**
     * 获取套餐列表数据
     *
     * @return
     */
    @RequestMapping("/getSetmeal")
    public Result getSetmeal() {
        try {
            //结果需要返回一个list集合  jiangdp
            List<Setmeal> setmealList = new ArrayList<>();
            // 为了减少频繁的操作数据库，先去查找redis
            // 1.把套餐从redis中取出放入list中
            Jedis jedis = jedisPool.getResource();
            // 放入的套餐数据的key为setmealList，如果存在就取出
            if (jedis.exists("setmealList")) {
                Setmeal setmeal = null;
                Set<String> list = jedis.smembers("setmealList");
                for (String s : list) {
                    setmeal = new Setmeal();
                    List<String> stringList = jedis.hmget(s, "id", "name", "age", "sex", "img", "remark");
                    setmeal.setId(Integer.parseInt(stringList.get(0)));
                    setmeal.setName(stringList.get(1));
                    setmeal.setAge(stringList.get(2));
                    setmeal.setSex(stringList.get(3));
                    setmeal.setImg(stringList.get(4));
                    setmeal.setRemark(stringList.get(5));
                    setmealList.add(setmeal);
                }
            }
            /**
             * @description:如果redis中不存在就去查找数据，并且把返回的结果放入redis中暂存
             * @author:jiangdp
             * @Date:Created in 2019/7/29
             */
            if (setmealList.size() == 0 || setmealList == null) {
                setmealList = setmealService.findAll();
                for (Setmeal setmeal1 : setmealList) {
                    Map<String, String> map = new HashMap<String, String>();
                    jedisPool.getResource().sadd("setmealList", "setmealList" + setmeal1.getId());
                    map.put("id", setmeal1.getId() + "");
                    map.put("name", setmeal1.getName());
                    map.put("code", setmeal1.getCode());
                    map.put("helpCode", setmeal1.getHelpCode());
                    map.put("sex", setmeal1.getSex());
                    map.put("age", setmeal1.getAge());
                    map.put("price", setmeal1.getPrice() + "");
                    map.put("remark", setmeal1.getRemark());
                    map.put("img", setmeal1.getImg());
                    jedisPool.getResource().hmset("setmealList" + setmeal1.getId(), map);
                }
            }


            return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmealList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * @author:jiangdp
     * description:根据套餐id查找套餐详情
     * @param id
     * @Date 2019-7-30
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {

            //创建jedis,setmeal对象
            //先从redis中获取数据
            String setmealRedis = jedisPool.getResource().get(RedisConst.SETMEAL_GROUP_ITEM+id);
            Setmeal setmeal = null;
            // 如果redis中的数据为空
            if (setmealRedis == null) {
                // 取数据库中的数据
                setmeal = setmealService.findById(id);
                // 格式化数据
                setmealRedis = new ObjectMapper().writeValueAsString(setmeal);
                // 放入redis中
                jedisPool.getResource().set(RedisConst.SETMEAL_GROUP_ITEM+id, setmealRedis);

            } else {
                // 如果redis中有数据，格式化后直接赋值给setmeal
                setmeal = new ObjectMapper().readValue(setmealRedis, Setmeal.class);

            }

            return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_SETMEAL_FAIL);
        }
    }


}
